package com.jiaquyun.jcps.ui.mine

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.base.ViewModeActivity
import com.jiaquyun.jcps.entity.OrderTodayListEntity
import com.jiaquyun.jcps.mode.vm.TodayViewModel
import com.lemlin.network.HttpServiceStatus
import kotlinx.android.synthetic.main.activity_today_order.*
import java.lang.StringBuilder

class TodayOrderActivity : ViewModeActivity<TodayViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, TodayOrderActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val orderLogAdapter =
        object : BaseQuickAdapter<OrderTodayListEntity, BaseViewHolder>(R.layout.item_order_today) {
            override fun convert(holder: BaseViewHolder, item: OrderTodayListEntity) {
                holder.setText(
                    R.id.tvTodayOrderNo,
                    StringBuilder().append("订单编号 ").append(item.order_sn)
                )
                    .setText(R.id.tvTodayAddressStart, item.start_address)
                    .setText(R.id.tvTodayAddressEnd, item.end_address)
                    .setText(R.id.tvTodayPrice, StringBuilder().append("￥").append(item.price))
                    .setText(
                        R.id.tvTodayTime,
                        TimeUtils.millis2String((item.service_time * 1000), "hh:mm")
                    )
            }
        }

    override fun viewModel(): TodayViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_today_order

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.colorWhite, view = flTodayOrderBar)
    }


    override fun initView() {

        ivTodayOrderBack.setOnClickListener {
            finish()
        }

        rvTodayOrderBody.adapter = orderLogAdapter
        orderLogAdapter.setOnItemClickListener { _, _, position ->
            val itemData: OrderTodayListEntity = orderLogAdapter.getItem(position)
            TodayOrderDetailsActivity.open(activity = this, orderId = itemData.id.toString())
        }

        smartRefreshHeader.setColorSchemeResources(R.color.colorMain)

        smartRefreshLayout.setOnRefreshListener {
            viewModel.orderLog()
        }
        smartRefreshLayout.setOnLoadMoreListener {
            viewModel.orderLog(isLoadMore = true)
        }
    }

    override fun initData() {
        smartRefreshLayout.autoRefresh()
    }

    override fun initViewMode() {
        viewModel.orderTodayListLiveData.observe(this, Observer { response ->
            if (response.statusCode == HttpServiceStatus.Status.FAILURE) {
                smartRefreshLayout.finishRefresh(false)
                smartRefreshLayout.finishLoadMore(false)
                toast(response.statusTip)
            }
            if (response.statusCode == HttpServiceStatus.Status.SUCCESS) {
                smartRefreshLayout.finishRefresh()
                smartRefreshLayout.finishLoadMore()
                response.httpData?.let {
                    if (it.loadMore) orderLogAdapter.addData(it.data)
                    else orderLogAdapter.setList(it.data)
                }
            }
        })
    }
}